home *** CD-ROM | disk | FTP | other *** search
- (* (* $VER: fRealIO 1.1 (21-May-94) Copyright © by Lars Düning *) *)
-
- MODULE fRealIO;
-
- (*---------------------------------------------------------------------------
- ** File-IO of REAL numbers for Amiga-Oberon.
- **
- ** Copyright © 1991-1994 Lars Düning - All rights reserved.
- ** Permission granted for non-commercial use.
- **---------------------------------------------------------------------------
- ** CREDIT:
- ** This module evolved from RealIO of Amiga-Oberon v1.17.1
- **---------------------------------------------------------------------------
- ** Oberon-2: Amiga-Oberon v3.10, F. Siebert / A+L AG
- **---------------------------------------------------------------------------
- ** [lars] Lars Düning; Am Wendenwehr 25; D-38114-Braunschweig;
- ** Germany; Tel. 49-531-345692
- **---------------------------------------------------------------------------
- ** 01-Jan-91 [lars]
- ** 21-May-04 [lars] Moved ParseReal() herein.
- **---------------------------------------------------------------------------
- *)
-
- IMPORT
- (* $IF Debug *) Debug, (* $END *)
- fio, fs:FSystem, rc: RealConversions;
-
- (*-------------------------------------------------------------------------*)
- PROCEDURE ParseReal * ( VAR in : fio.File; VAR str : ARRAY OF CHAR) : BOOLEAN;
-
- (* Parse a REAL number from a file.
- **
- ** Arguments:
- ** in : the file to read from.
- ** str : buffer taking the number string.
- **
- ** Result:
- ** TRUE on success, else FALSE with in.status denoting the error.
- ** If FALSE is returned, but in.status is 'ok', then no correct number
- ** could be read.
- **
- ** Leading whitespace are ignored, trailing linebreaks are not read.
- *)
-
- VAR
- i : INTEGER;
- ch : CHAR;
- isReal : BOOLEAN;
-
- BEGIN
- isReal := FALSE;
- IF LEN(str) = 0 THEN RETURN FALSE; END;
- i := 0;
- WHILE i < LEN(str) DO str[i] := 0X; END;
- i := 0;
-
- (* Skip any preceeding spaces/tabs *)
- REPEAT
- IF ~fio.Read(in, ch) THEN RETURN FALSE; END;
- UNTIL (ch # ' ') AND (ch # '\t');
-
- (* Mantissen-Vorzeichen ? *)
- IF (ch = "+") OR (ch = "-") THEN
- str[i] := ch; INC (i); IF i = LEN (str) THEN RETURN FALSE; END;
- IF ~fio.Read(in, ch) THEN RETURN FALSE; END;
- END;
-
- (* Mantisse - integer part *)
- LOOP
- CASE ch OF
- '0'..'9':
- isReal := TRUE;
- str[i] := ch; INC(i); IF i = LEN(str) THEN RETURN isReal; END;
- IF ~fio.Read(in, ch) THEN RETURN FALSE; END;
- ELSE EXIT END;
- END;
-
- (* Decimal point with mantisse fraction? *)
- IF ch = "." THEN
- isReal := FALSE;
- str[i] := ch; INC(i); IF i = LEN(str) THEN RETURN FALSE; END;
- IF ~fio.Read(in, ch) THEN RETURN FALSE; END;
-
- LOOP
- CASE ch OF
- '0'..'9':
- isReal := TRUE;
- str[i] := ch; INC(i); IF i = LEN(str) THEN RETURN isReal; END;
- IF ~fio.Read(in, ch) THEN RETURN FALSE; END;
- ELSE EXIT END;
- END;
- END;
-
- (* Exponent? (assuming a legal float preceeded) *)
- IF isReal AND ((ch = "E") OR (ch = "e")) THEN
- isReal := FALSE;
- str[i] := ch; INC(i); IF i = LEN(str) THEN RETURN FALSE; END;
- IF ~fio.Read(in, ch) THEN RETURN FALSE; END;
-
- (* Sign of exponent? *)
- IF (ch = "+") OR (ch = "-") THEN
- str[i] := ch; INC (i); IF i = LEN (str) THEN RETURN FALSE; END;
- IF ~fio.Read(in, ch) THEN RETURN FALSE; END;
- END;
-
- LOOP
- CASE ch OF
- '0'..'9':
- isReal := TRUE;
- str[i] := ch; INC(i); IF i = LEN(str) THEN RETURN isReal; END;
- IF ~fio.Read(in, ch) THEN RETURN FALSE; END;
- ELSE EXIT END;
- END;
- END;
-
- RETURN fs.Backward(in, 1) & isReal;
- END ParseReal;
-
- (*-------------------------------------------------------------------------*)
- PROCEDURE writeReal * {"fRealIO.WriteReal"}
- ( VAR f : fio.File
- ; r : REAL
- ; v, n : INTEGER
- ; exp : BOOLEAN
- );
- PROCEDURE WriteReal * ( VAR f : fio.File
- ; r : REAL
- ; v, n : INTEGER
- ; exp : BOOLEAN
- ): BOOLEAN;
-
- (* Write a REAL number into a file.
- **
- ** Arguments:
- ** f : the file to write to.
- ** r : the number to write.
- ** v : number of digits in front of the '.'
- ** n : number of digits after the '.'
- ** exp: if TRUE, the 'E' notation is used when appropriate.
- **
- ** Result:
- ** TRUE on success, else FALSE with out.status denoting the error.
- ** If FALSE is returned, but out.status is 'ok', then the number is
- ** larger than the allowed number of digits.
- *)
-
- VAR
- str: ARRAY 256 OF CHAR;
-
- BEGIN
- IF rc.RealToString(r,str,v,n,exp) THEN
- RETURN fio.WriteString(f,str);
- ELSE
- RETURN FALSE;
- END;
- END WriteReal;
-
- (*-------------------------------------------------------------------------*)
- PROCEDURE readReal * {"fRealIO.ReadReal"}(VAR f : fio.File; VAR r: REAL);
- PROCEDURE ReadReal * (VAR f : fio.File; VAR r: REAL): BOOLEAN;
-
- (* Read a REAL number from a file.
- **
- ** Arguments:
- ** f : the file to read from.
- ** r : variable to take the number read.
- **
- ** Result:
- ** TRUE on success, else FALSE with out.status denoting the error.
- ** If FALSE is returned, but out.status is 'ok', then no correct number
- ** could be read.
- ** r: the number read.
- *)
-
- VAR
- str: ARRAY 256 OF CHAR;
-
- BEGIN
- IF ParseReal(f, str) THEN
- RETURN rc.StringToReal(str,r);
- END;
- RETURN FALSE;
- END ReadReal;
-
- END fRealIO.
-
- (***************************************************************************)
-